﻿using DataManage;
using ServiceLayerManagement;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CANCommunication
{
    /// <summary>
    /// ZLG方法引用
    /// </summary>
    public static class MethodInvoke
    {
        #region 接口方法引用
        /// <summary>
        /// 打开设备
        /// </summary>
        /// <param name="Reserved">预留，常为0</param>
        /// <returns>INVALID_DEVICE_HANDLE表示操作失败</returns>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr ZCAN_OpenDevice(uint DeviceType, uint DeviceInd, uint Reserved);
        /// <summary>
        /// 关闭设备
        /// </summary>
        /// <param name="device_handle">ZCAN_OpenDevice返回的句柄</param>
        /// <returns>STATUS_OK表示操作成功，STATUS_ERR表示操作失败</returns>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern UInt32 ZCAN_CloseDevice(IntPtr device_handle);
        /// <summary>
        /// 初始化通道
        /// </summary>
        /// <param name="device_handle">ZCAN_OpenDevice返回的句柄</param>
        /// <param name="can_index">CAN通道索引</param>
        /// <param name="pInitConfig">初始化结构</param>
        /// <returns>　为INVALID_CHANNEL_HANDLE表示操作失败，否则表示操作成功</returns>
        [DllImport("zlgcan.dll")]
        public static extern IntPtr ZCAN_InitCAN(IntPtr device_handle, uint can_index, IntPtr pInitConfig);
        /// <summary>
        /// 启动CAN通道
        /// </summary>
        /// <returns>STATUS_OK表示操作成功，STATUS_ERR表示操作失败</returns>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern UInt32 ZCAN_StartCAN(IntPtr channel_handle);
        /// <summary>
        /// 复位CAN通道
        /// <para>若CAN通道已启动，需调用该函数进行通道复位，再进行启动通道的操作</para>
        /// </summary>
        /// <returns></returns>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern UInt32 ZCAN_ResetCAN(IntPtr channel_handle);
        /// <summary>
        /// 用于发送CAN报文
        /// </summary>
        /// <param name="pTransmit">结构体ZCAN_Transmit_Data数组的首指针</param>
        /// <param name="len">报文数目</param>
        /// <returns>返回实际发送成功的报文数目</returns>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern uint ZCAN_Transmit(IntPtr channel_handle, IntPtr pTransmit, uint len);
        /// <summary>
        /// 用于发送CANFD报文
        /// </summary>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern uint ZCAN_TransmitFD(IntPtr channel_handle, IntPtr pTransmit, uint len);
        /// <summary>
        /// 获取缓冲区中CAN或CANFD报文数目
        /// </summary>
        /// <param name="type">获取CAN或CANFD报文，0=CAN，1=CANFD</param>
        /// <returns>返回报文数目</returns>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern uint ZCAN_GetReceiveNum(IntPtr channel_handle, byte type);
        /// <summary>
        /// 接收CAN报文，建议使用ZCAN_GetReceiveNum确保缓冲区有数据再使用
        /// </summary>
        /// <param name="data">结构体ZCAN_Receive_Data数组的首指针</param>
        /// <param name="len">数组长度（本次接收的最大报文数目，实际返回值小于等于这个值）</param>
        /// <param name="wait_time">缓冲区无数据，函数阻塞等待时间，单位毫秒。若为-1则表示无超时，一直等待，默认值为-1</param>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern uint ZCAN_Receive(IntPtr channel_handle, IntPtr data, uint len, int wait_time = -1);
        /// <summary>
        /// 接收CANFD数据，建议使用ZCAN_GetReceiveNum确保缓冲区有数据再使用
        /// </summary>
        /// <param name="data">结构体ZCAN_ReceiveFD_Data数组的首指针</param>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern uint ZCAN_ReceiveFD(IntPtr channel_handle, IntPtr data, uint len, int wait_time = -1);
        /// <summary>
        /// 1.设置CANFD的协议类型
        /// <para>2.设置波特率</para>
        /// </summary>
        /// <returns></returns>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern uint ZCAN_SetValue(IntPtr device_handle, string path, byte[] value);
        /// <summary>
        /// 检测设备是否在线
        /// <para>仅支持USB系列设备</para>
        /// </summary>
        /// <param name="OpenDeviceResult">ZCAN_OpenDevice返回的结果值</param>
        /// <returns></returns>
        [DllImport("zlgcan.dll")]
        public static extern uint ZCAN_IsDeviceOnLine(IntPtr device_handle);
        /// <summary>
        /// 返回当前通道的错误码
        /// </summary>
        /// <param name="pErrInfo">ZCAN_CHANNEL_ERROR_INFO结构体的首指针</param>
        /// <returns></returns>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern uint ZCAN_ReadChannelErrInfo(IntPtr channel_handle, IntPtr pErrInfo);
        /// <summary>
        /// 清空缓冲区
        /// </summary>
        /// <param name="pErrinfo">错误信息结构，详见 ZCAN_CHANNEL_ERROR_INFO 结构体</param>
        /// <returns></returns>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern UInt32 ZCAN_ClearBuffer(IntPtr channel_handle);
        /// <summary>
        /// 读取通道的状态信息
        /// </summary>
        /// <param name="pCANStatus">状态信息结构，详见 ZCAN_CHANNEL_STATUS 结构体</param>
        /// <returns>STATUS_OK表示操作成功，STATUS_ERR表示操作失败</returns>
        [DllImport("zlgcan.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern UInt32 ZCAN_ReadChannelStatus(IntPtr channel_handle, IntPtr pCANStatus);
        #endregion

       
    }
}
